#include "drv_adc.h"

void init_adc(void){
    /*Configure ADC*/
    ADCON0 = 0x00;  //Reset Register
    /*Enable ADC*/
    ADCON0 = (ADC_ON);

    /*Configure ADC Ref*/
    ADCON1 &= (ADC_VSS_REF & ADC_VDD_REF);

    /*Configure ADC Ports*/
    ADCON1 &= (CLEAR_ADC_PORTS);
    /*AN0 - AN7 Activated*/
    ADCON1 |= 0x07;

    /*Configure ADC Format Result*/
    ADCON2 |= RIGHT_JUST;
    /*Clear A/D ADQ Time*/
    ADCON2 &= (~(0x07 << 3));
    /*Configure ADQ Time*/
    ADCON2 |= TAD_0;
    /*Clear Conversion Select Bits*/
    ADCON2 &= (~(0x07));
    /*Configure Conversion Select Bits*/
    ADCON2 |= (FRC);
}

T_UWORD get_adc(T_UBYTE lub_AdcChannel){
    //Clear channel to get
    ADCON0 &= (~(0x0F << 2));
    //Set channel to get
    ADCON0 |= (lub_AdcChannel << 2);
    //Start conversion
    GO_nDONE = 1;
    //Wait until conversion is complete
    while(GO_nDONE == 1){

    }
    //Read Result
    return(((T_UWORD)ADRESH << 8)|(T_UWORD)ADRESL);
}

